package com.jeeplus.modules.common.excel.common.validator.impl;

import com.jeeplus.common.utils.StringUtil;
import com.jeeplus.modules.common.excel.common.anno.DictTransformer;
import com.jeeplus.modules.common.excel.common.validator.abs.AbstractValidator;
import com.jeeplus.modules.common.utils.DictionaryUtils;
import com.jeeplus.modules.da.utils.XgDictUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

/**
 * 字典校验
 * @author wang.yishu
 */
public class DictValidator extends AbstractValidator {
    private DictTransformer dictTransformer;

    @Override
    public boolean canValidate(Field field) {
        dictTransformer = field.getDeclaredAnnotation(DictTransformer.class);
        return dictTransformer != null;
    }

    @Override
    public String toValidate(Object entity, Field field) {
        String dictType = dictTransformer.dictType();
        String transformTo = dictTransformer.transformTo();
        String dictValue = null;
        field.setAccessible(true);
        try {
            Object dictLabel = field.get(entity);
            if (dictLabel == null) return null;
            String dictLabelStr = null;
            if (dictLabel instanceof  String) {
                dictLabelStr = dictLabel.toString();
            } else {
                return "字典值类型错误！";
            }
            boolean hasLabel = false;
            String forCodeFieldName = this.dictTransformer.forCode();
            String dictCodeValue = "";
            if (StringUtil.isNotBlank(forCodeFieldName)) {
                Field forCodeField = entity.getClass().getDeclaredField(forCodeFieldName);
                forCodeField.setAccessible(true);
                dictCodeValue = (String) forCodeField.get(entity);
            }
            if (StringUtil.isNotBlank(dictType)) {
                List<Map<String, Object>> dict = XgDictUtils.getDict(dictType);
                dictValue = DictionaryUtils.getDictValue(dictType, dictLabelStr, dict);
                if (StringUtil.isBlank(dictValue)) {
                    return dictTransformer.msg();
                }
                if (StringUtil.isNotBlank(dictCodeValue)) {
                    if (!dictValue.equals(dictCodeValue)) {
                        return dictTransformer.msg();
                    }
                }
                hasLabel = true;
            } else {
                String[] codes = dictTransformer.dictCodes();
                String[] labels = dictTransformer.dictLabels();
                int index = 0;
                if (codes != null && labels != null && codes.length > 0 && labels.length > 0) {
                    for (int i = 0; i < labels.length; i++) {
                        if (labels[i].equals(dictLabelStr)) {
                            index = i;
                            hasLabel = true;
                            break;
                        }
                    }
                    if (index < codes.length) {
                        dictValue = codes[index];
                    }
                    if (!hasLabel) {
                        return dictTransformer.msg();
                    }
                }
            }
            if (StringUtil.isNotBlank(transformTo) && hasLabel) {
                Field transformToField = entity.getClass().getDeclaredField(transformTo);
                transformToField.setAccessible(true);
                transformToField.set(entity, dictValue);
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return "";
    }
}
